<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests\AdminloginRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    public function getLogin()
    {
        return view('admin.login.login');
    }
	public function postLogin(AdminloginRequest $request)
    {
        //检测用户名是否一致
        $res = DB::table('user')->where('username',$request->input('username'))->first();
        //检测用户是否为空
        if(!$res){
            return back()->with('error','用户或密码错误');
        }
        // 检测密码是否一致
        if(Hash::check($request->input('password'),$res->password)){
            if($res->qx == 1){
                return back()->with('error',"普通用户没有进入权限,请联系管理员");
            }else{
                session(['ID'=>$res->id]);
                session(['QX'=>$res->qx]);
                return redirect('/admin/index')->with('success','欢迎'.$res->username.'登陆');
            }
        }else{
            return back()->with('error','用户名或密码不正确');
        }
    }

    public function getLoginout(Request $request)
    {
        $request->session()->forget('ID');
        return redirect('/admin/login/login')->with('error','您已退出登陆');
    }
}
